﻿@page "/"
@inject ILocalStorageService LocalStorage
@inject ILogService LogService

<PageTitle>AI学习平台 - 仪表盘</PageTitle>

<div>
    <Title Level="2">仪表盘</Title>
    <Text Type="secondary">欢迎使用AI辅助学习平台</Text>
    
    <Row Gutter="16" Style="margin-top: 24px;">
        <Col Span="6">
            <Card Hoverable>
                <Statistic Title="孩子档案" Value="@childrenCount">
                    <PrefixTemplate>
                        <Icon Type="user" />
                    </PrefixTemplate>
                </Statistic>
            </Card>
        </Col>
        <Col Span="6">
            <Card Hoverable>
                <Statistic Title="已生成试卷" Value="@examsCount">
                    <PrefixTemplate>
                        <Icon Type="file-text" />
                    </PrefixTemplate>
                </Statistic>
            </Card>
        </Col>
        <Col Span="6">
            <Card Hoverable>
                <Statistic Title="API状态" Value="@apiStatus">
                    <PrefixTemplate>
                        <Icon Type="api" />
                    </PrefixTemplate>
                </Statistic>
            </Card>
        </Col>
        <Col Span="6">
            <Card Hoverable>
                <Statistic Title="系统日志" Value="@logsCount">
                    <PrefixTemplate>
                        <Icon Type="file" />
                    </PrefixTemplate>
                </Statistic>
            </Card>
        </Col>
    </Row>
    
    <Row Gutter="16" Style="margin-top: 24px;">
        <Col Span="12">
            <Card Title="最近生成的试卷" Size="small">
                @if (recentExams?.Any() == true)
                {
                    @foreach (var exam in recentExams)
                    {
                        <div style="padding: 8px 0; border-bottom: 1px solid #f0f0f0;">
                            <div style="font-weight: 500;">@exam.Title</div>
                            <div style="color: #666; font-size: 12px;">@exam.CreateTime.ToString("yyyy-MM-dd HH:mm")</div>
                        </div>
                    }
                }
                else
                {
                    <Empty Description="@("暂无试卷")" />
                }
            </Card>
        </Col>
        <Col Span="12">
            <Card Title="快速操作" Size="small">
                <Space Direction="DirectionVHType.Vertical" Style="width: 100%;">
                    <SpaceItem>
                        <Button Type="@ButtonType.Primary" Icon="file-add" OnClick="NavigateToGenerate" Block>
                            生成新试卷
                        </Button>
                    </SpaceItem>
                    <SpaceItem>
                        <Button Icon="user-add" OnClick="NavigateToAddChild" Block>
                            添加孩子档案
                        </Button>
                    </SpaceItem>
                    <SpaceItem>
                        <Button Icon="setting" OnClick="NavigateToSettings" Block>
                            系统设置
                        </Button>
                    </SpaceItem>
                </Space>
            </Card>
        </Col>
    </Row>
</div>

@code {
    private int childrenCount = 0;
    private int examsCount = 0;
    private string apiStatus = "未配置";
    private int logsCount = 0;
    private List<ExamPaper>? recentExams;
    
    protected override async Task OnInitializedAsync()
    {
        await LoadDashboardData();
    }
    
    private async Task LoadDashboardData()
    {
        try
        {
            // 加载孩子档案数量
            var children = await LocalStorage.GetChildrenAsync();
            childrenCount = children.Count;
            
            // 加载试卷数量
            var exams = await LocalStorage.GetExamPapersAsync();
            examsCount = exams.Count;
            
            // 获取最近5个试卷
            recentExams = exams.OrderByDescending(e => e.CreateTime).Take(5).ToList();
            
            // 检查API配置状态
            var apiConfig = await LocalStorage.GetApiConfigAsync();
            if (apiConfig != null && !string.IsNullOrEmpty(apiConfig.ApiKey))
            {
                apiStatus = apiConfig.IsTestSuccess ? "已连接" : "配置异常";
            }
            
            // 加载日志数量
            var logs = await LocalStorage.GetLogsAsync();
            logsCount = logs.Count;
            
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await LogService.LogErrorAsync($"加载仪表盘数据失败: {ex.Message}", "Dashboard");
        }
    }
    
    private void NavigateToGenerate()
    {
        // TODO: 导航到试卷生成页面
    }
    
    private void NavigateToAddChild()
    {
        // TODO: 导航到添加孩子页面
    }
    
    private void NavigateToSettings()
    {
        // TODO: 导航到设置页面
    }
}
